package com.yc;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Test7_telnet_baidu {
    public static void main(String[] args) {
        String website = "www.baidu.com";
        int port=80;
        String http="GET/HTTP/1.0\r\n\r\n";////应用层协议
        try (Socket s = new Socket(website, port);
             OutputStream oos = s.getOutputStream();
             InputStream iis = s.getInputStream();
        ) {
            oos.write(http.getBytes());
            oos.flush();
            //针对内存的缓冲流   内存字节输出流
            ByteArrayOutputStream baos =new ByteArrayOutputStream();
            byte[] bt = new byte[10*1024];
            int length= -1;
            while( (length =iis.read(bt,0,bt.length))!=-1){
                baos.write(bt,0,length);
            }
            baos.flush();
            byte[] bb=baos.toByteArray();
            String str =new String(bb);
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
